//科目资讯控制器。
Ext.define("ES.controller.InformationController",{
    extend:"Ext.app.Controller",
    models:["InformationModel","CatalogModel"],
    stores:["ExamTypeStore","ExamStatusStore","InformationStore","CatalogStore"],
    views:["information.InformationList","information.InformationEdit"],
    init:function(){
        this.control({
            "InformationTabList":{
                afterrender:function(cmp){
                    var typeStore = this.getExamTypeStoreStore();
                    var statusStore = this.getExamStatusStoreStore();
                    if(typeStore != null && statusStore != null){
                        typeStore.load({
                            callback:function(records, operation, success){
                                if(success){
                                    cmp.setEnumTypeStore(typeStore);
                                }
                            }
                        });
                        statusStore.load({
                            callback:function(records, operation, success){
                                if(success){
                                    cmp.setEnumStatusStore(statusStore);     
                                }                  
                            } 
                        });
                    }
                },
                itemdblclick:this.onEdit
            },
            "InformationTabList button[action=btnQuery]":{
                click:this.onSearch
            },
            "InformationTabList button[action=btnAdd]":{
                click:this.onAdd
            },
            "InformationTabList button[action=btnDelete]":{
                click:this.onDelete
            },
            "informationEdit combo[name=catalogID]":{
                beforerender:function(combo){
                    var store = combo.getStore();
                    if(store){
                        store.load();
                    }
                }
            },
            "informationEdit button[action=btnSave]":{
                click: this.onSave
            },
            "informationEdit button[action=btnCancel]":{
                click:function(btn){
                    var win = btn.up("informationEdit");
                    if(win){
                        win.close();
                    }
                }
            }
        });
    },
    onSearch:function(btn){
        var toolbar =  btn.ownerCt;
        var grid = toolbar.ownerCt;       
        var st = grid.getStore();
        st.loadPage(1,{
            params:{
                infoTitle:toolbar.getComponent(1).getValue()
            }
        });
    },
    onAdd:function(){
        var win = Ext.widget("informationEdit");
        if(win){
            win.show();
        }
    },
    onEdit:function(view, record, item,index,e,opt){
        var win =Ext.widget("informationEdit");
        if(win){
            win.show();
            var el = win.getEl(); 
            el.mask("正在加载数据，请稍后....");
            if(record){
                var frm = win.down("form");
                if(frm){
                    frm.loadRecord(record);
                }
            }
            el.unmask(); 
        }
    },
    onSave:function(btn){
        var win = btn.up("informationEdit");
        var form = btn.up("form").getForm();
        if(form.isValid()){
            var store = this.getInformationStoreStore();
            var record = form.getRecord();
            var values = form.getValues();
            if(record){
                record.set(values);
            }else{
                record = this.getInformationModelModel().create();
                record.set(values);
                store.insert(0,record);
            }
            store.sync();
            win.close();
        }
    },
    onDelete:function(btn){
        Ext.MessageBox.confirm("删除提示","确认要删除选中的数据吗？", function(cfm){
            if(cfm == "yes"){
                var grid = btn.ownerCt.ownerCt;
                var data = grid.getSelectionModel().getSelection();
                if(data.length == 0){
                    Ext.Msg.alert("提示","您最少要选择一条数据!");
                }else{
                    var el = grid.getEl()
                    el.mask("正在删除数据，请稍后...");
                    var st = grid.getStore();
                    Ext.Array.each(data, function(record){
                        st.remove(record);
                    })
                    st.sync();
                    el.unmask();
                }
            }
        });
    }
})